from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QAction
import sys


class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 设置窗口标题和大小
        self.setWindowTitle('PyQt 菜单示例')
        self.resize(500, 300)

        # 创建菜单栏
        menu_bar = QMenuBar(self)
        self.setMenuBar(menu_bar)

        # 创建文件菜单
        file_menu = QMenu('文件', self)
        menu_bar.addMenu(file_menu)

        # 创建文件菜单中的菜单项
        open_action = QAction('打开', self)
        save_action = QAction('保存', self)
        exit_action = QAction('退出', self)
        open_action.setIcon(QIcon("../icon/save.png"))
        # 将菜单项添加到文件菜单
        file_menu.addAction(open_action)
        file_menu.addAction(save_action)
        file_menu.addSeparator()
        file_menu.addAction(exit_action)

        # 创建编辑菜单
        edit_menu = QMenu('编辑', self)
        menu_bar.addMenu(edit_menu)

        # 创建编辑菜单中的菜单项
        cut_action = QAction('剪切', self)
        copy_action = QAction('复制', self)
        paste_action = QAction('粘贴', self)

        # 将菜单项添加到编辑菜单
        edit_menu.addAction(cut_action)
        edit_menu.addAction(copy_action)
        edit_menu.addAction(paste_action)

        # 为退出菜单项连接信号槽，点击退出时关闭应用程序
        exit_action.triggered.connect(self.close)
        open_action.triggered.connect(self.printAction)

    def printAction(self):
        print(1111)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())
